package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.enums.UserPraiseEnum;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.BdmUserPraise;
import com.ruoyi.system.domain.dto.UserPraiseDto;
import com.ruoyi.system.mapper.BdmUserPraiseMapper;
import com.ruoyi.system.service.IBdmUserPraiseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * 点赞Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-12-12
 */
@Service
public class BdmUserPraiseServiceImpl implements IBdmUserPraiseService
{
    @Autowired
    private BdmUserPraiseMapper bdmUserPraiseMapper;

    /**
     * 查询点赞
     * 
     * @param id 点赞主键
     * @return 点赞
     */
    @Override
    public BdmUserPraise selectBdmUserPraiseById(Long id)
    {
        return bdmUserPraiseMapper.selectBdmUserPraiseById(id);
    }

    /**
     * 查询点赞列表
     * 
     * @param bdmUserPraise 点赞
     * @return 点赞
     */
    @Override
    public List<BdmUserPraise> selectBdmUserPraiseList(BdmUserPraise bdmUserPraise)
    {
        return bdmUserPraiseMapper.selectBdmUserPraiseList(bdmUserPraise);
    }

    /**
     * 新增点赞
     * 
     * @param bdmUserPraise 点赞
     * @return 结果
     */
    @Override
    public int insertBdmUserPraise(BdmUserPraise bdmUserPraise)
    {
        bdmUserPraise.setCreateTime(DateUtils.getNowDate());
        return bdmUserPraiseMapper.insertBdmUserPraise(bdmUserPraise);
    }

    /**
     * 修改点赞
     * 
     * @param bdmUserPraise 点赞
     * @return 结果
     */
    @Override
    public int updateBdmUserPraise(BdmUserPraise bdmUserPraise)
    {
        bdmUserPraise.setUpdateTime(DateUtils.getNowDate());
        return bdmUserPraiseMapper.updateBdmUserPraise(bdmUserPraise);
    }

    /**
     * 批量删除点赞
     * 
     * @param ids 需要删除的点赞主键
     * @return 结果
     */
    @Override
    public int deleteBdmUserPraiseByIds(Long[] ids)
    {
        return bdmUserPraiseMapper.deleteBdmUserPraiseByIds(ids);
    }

    /**
     * 删除点赞信息
     * 
     * @param id 点赞主键
     * @return 结果
     */
    @Override
    public int deleteBdmUserPraiseById(Long id)
    {
        return bdmUserPraiseMapper.deleteBdmUserPraiseById(id);
    }


    /**
     * 新增/删除点赞
     * @param userPraiseDto
     * @return
     */
    @Override
    public int savePraise(UserPraiseDto userPraiseDto) {
        BdmUserPraise praise = BeanUtil.copyProperties(userPraiseDto, BdmUserPraise.class);
        if (Objects.equals(userPraiseDto.getLike(), UserPraiseEnum.LIKE.getCode())) {
            praise.setCreateTime(DateUtils.getNowDate());
            return bdmUserPraiseMapper.insertBdmUserPraise(praise);
        }else {
            return bdmUserPraiseMapper.deleteBdmUserPraiseBySysUidAndItemId(userPraiseDto);
        }
    }

    /**
     * 是否点赞 （1点赞，2无）
     * @param userPraiseDto
     * @return
     */
    @Override
    public Long isLike(UserPraiseDto userPraiseDto) {
        BdmUserPraise praise = new BdmUserPraise();
        praise.setSysUid(userPraiseDto.getSysUid());
        praise.setItemId(userPraiseDto.getItemId());
        List<BdmUserPraise> praiseList = bdmUserPraiseMapper.selectBdmUserPraiseList(praise);
        if (StringUtils.isNotEmpty(praiseList)) {
            return 1L;
        }
        return 2L;
    }
}
